package utils;

import java.util.*;
import java.io.*;

public class DBHandler {

	public DBHandler()  {
		try {
				rawWrite("UID", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addCoin(int coinAmmount) {
		rawWrite("COIN", Integer.toString(coinAmmount));
	}

	public String getCoin() throws Exception {
		return rawRead("coin");
	}

	public void addItem(String itemName, float itemCount) {
		rawWrite(itemName, Float.toString(itemCount));
	}

	public String getItem(String itemName) throws Exception {
		return rawRead(itemName);
	}

	public String getUID() {
		try {
			return rawRead("UID");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private void rawWrite(String key, String value) {
		Properties prop = new Properties();
		prop.setProperty(key, value);
		try {
			FileOutputStream fos = new FileOutputStream("spacegame.xml");
			prop.storeToXML(fos, "tlc-spacegameDB");
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
			
		}

	}

	private String rawRead(String key) throws Exception {
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream("spacegame.xml");
		prop.loadFromXML(fis);
		return prop.getProperty(key);
	}

}
